package com.ibm.servlets;


import com.ibm.entity.User;
import com.ibm.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.List;

@WebServlet("/UserUpdataServlet")
public class UserUpdataServlet extends HttpServlet {
    private UserService userService=new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //1.接收管理员修改后的信息
        request.setCharacterEncoding("utf-8");


        int userId = Integer.parseInt(request.getParameter("userId"));  //原始管理员ID
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("loginPwd");
        int score = Integer.parseInt(request.getParameter("score"));

        User user = new User(userId,userName,pwd,score);


        //3.执行修改
        boolean b = userService.updateUser(user);

        //4.修改完成之后跳转到管理员列表页面（需要重新查询管理员列表和角色列表）
        String tips = b?"<label style='color:green'>用户修改成功！</label>"
                :"<label style='color:red'>用户修改失败！</label>";
        //当再次回到admin_manager_list.jsp页面时，依然需要管理员列表和角色列表
        List<User> userList = UserService.listUser();


        request.setAttribute("tips",tips);
        request.setAttribute("userList",userList);

        request.getRequestDispatcher("member-show.jsp").forward(request,response);
    }

}
